local joyex__fankui = fk.CreateSkill {

  name = "joyex__fankui",

  tags = {  },

}



joyex__fankui:addEffect(fk.Damaged, {
  name = "joyex__fankui",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(joyex__fankui.name)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if self.cancel_cost or player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, joyex__fankui.name, data) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = joyex__fankui.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.suit == Card.Heart then
      local targets = table.map(table.filter(room.alive_players, function(p) return not p:isNude() end), Util.IdMapper)
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#joyex__fankui-choose", joyex__fankui.name, false)
      local to = room:getPlayerById(tos[1])
      local card = room:askForCardChosen(player, to, "he", joyex__fankui.name)
      room:obtainCard(player, card, false, fk.ReasonPrey)
    else
      local from = data.from
      if from and not from.dead and not from:isNude() then
        room:doIndicate(player.id, {from.id})
        local card = room:askForCardChosen(player, from, "he", joyex__fankui.name)
        room:obtainCard(player, card, false, fk.ReasonPrey)
      end
    end
  end,
})

return joyex__fankui